﻿using System;

namespace Shell.PocoDot.Configurations
{
    /// <summary>
    /// 表示两个 PocoDot 的关系的配置信息
    /// </summary>
    public sealed class RelationConfiguration
    {
        /// <summary>
        /// 构造一个新的实例
        /// </summary>
        /// <param name="mainPocoDot">指定的关系中的主 PocoDot</param>
        /// <param name="relationship">主 PocoDot 到从 PocoDot 之间的映射关系</param>
        /// <param name="relatedPocoDot">指定的关系中的从 PocoDot</param>
        /// <param name="reversedRelationship">从 PocoDot 到主 PocoDot 之间的映射关系</param>
        /// <param name="provider">此关系所基于的持久化配置信息</param>
        public RelationConfiguration(Type mainPocoDot, Relationships relationship,
            Type relatedPocoDot, Relationships reversedRelationship, ProviderConfiguration provider)
        {
            this.Relationship = relationship;
            this.ReversedRelationship = reversedRelationship;
            this.MainPocoDot = mainPocoDot;
            this.RelatedPocoDot = relatedPocoDot;
            this.ProviderConfiguration = provider;
        }

        /// <summary>
        /// 获取此关系所基于的持久化配置信息
        /// </summary>
        public ProviderConfiguration ProviderConfiguration { get; private set; }

        /// <summary>
        /// 获取此关系中的主 PocoDot
        /// </summary>
        public Type MainPocoDot { get; private set; }

        /// <summary>
        /// 获取主 PocoDot 到从 PocoDot 之间的映射关系
        /// </summary>
        public Relationships Relationship { get; private set; }

        /// <summary>
        /// 获取此关系中的从 PocoDot
        /// </summary>
        public Type RelatedPocoDot { get; private set; }

        /// <summary>
        /// 获取从 PocoDot 到主 PocoDot 之间的映射关系
        /// </summary>
        public Relationships ReversedRelationship { get; private set; }

        /// <summary>
        /// 决定指定的实例是否与当前实例相同
        /// </summary>
        /// <param name="obj">指定的实例</param>
        /// <returns>判决结果</returns>
        public override bool Equals(object obj)
        {
            var config = obj as RelationConfiguration;
            if (config == null)
            {
                return false;
            }
            return config.ProviderConfiguration == this.ProviderConfiguration
                && config.Relationship == this.Relationship
                && config.ReversedRelationship == this.ReversedRelationship
                && config.MainPocoDot == this.MainPocoDot
                && config.RelatedPocoDot == this.RelatedPocoDot;
        }

        /// <summary>
        /// 获取当前实例的哈希值
        /// </summary>
        /// <returns>哈希值</returns>
        public override int GetHashCode()
        {
            return this.MainPocoDot.GetHashCode() + this.RelatedPocoDot.GetHashCode() ;
        }
    }
}
